<?php

namespace app\shop\model\user;

use app\common\model\user\Visit as VisitModel;

/**
 * 收藏模型
 */
class Visit extends VisitModel
{
	/**
	 * 获取用户统计数量
	 */
	public function getVisitData($startDate, $endDate, $type)
	{
		$model = $this;
		if (!is_null($startDate)) {
			$model = $model->where('create_time', '>=', strtotime($startDate));
		}
		if (is_null($endDate)) {
			$model = $model->where('create_time', '<', strtotime($startDate) + 86400);
		} else {
			$model = $model->where('create_time', '<', strtotime($endDate) + 86400);
		}
		if ($type == 'visit_user') {
			$userIds = $model->distinct(true)->column('visitcode');
			return count($userIds);
		} else if ($type == 'visit_count') {
			return $model->count();
		}
		return 0;
	}

	/**
	 * 获取用户访问数据
	 */
	public function getVisitDataList($startDate, $endDate)
	{
		$model = $this;
		if (!is_null($startDate)) {
			$model = $model->where('create_time', '>=', strtotime($startDate));
		}
		if (is_null($endDate)) {
			$model = $model->where('create_time', '<', strtotime($startDate) + 86400);
		} else {
			$model = $model->where('create_time', '<', strtotime($endDate));
		}
		$userVlist = $model->with('getVisitName')->order("create_time asc")->select();
		$outdata = [];
		foreach ($userVlist as $key => $val) {
			if(!$val['getVisitName']['user_id']){
				unset($userVlist[$key]);
			}
		}
		$iii = 0;
		foreach ($userVlist as $key => $val) {
			$outdata[$iii]['id'] = $val['visit_id'];
			$outdata[$iii]['visiter_id'] = $val['getVisitName']['user_id'];
			$outdata[$iii]['visiter'] = $val['getVisitName']['nickName'];
			$outdata[$iii]['visiter_phone'] = $val['getVisitName']['mobile'];
			$outdata[$iii]['visit_pro'] = $val['content'];
			$outdata[$iii]['day'] = date('Y-m-d',strtotime($val['create_time']));
			$outdata[$iii]['time'] = date('H:i:s',strtotime($val['create_time']));
			$iii++;
		}
		return $outdata;
	}

	public function getVisitName(){
		return $this->belongsTo('app\\common\\model\\user\\User');
	}
}
